//
//  DetailViewController.m
//  糗百——王桂敏
//
//  Created by 王桂敏 on 15/5/30.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "DetailViewController.h"
#import "CommentModel.h"
#import "UserViewController.h"
#import "NetWorkEngine.h"
#import "TableViewCell.h"
#import "Define.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate,NetworkEngineDelegate>
@property (nonatomic,retain) UITableView *tableview;//表视图
@property (nonatomic,retain) NSMutableArray *dataSource;//数据源
@end

@implementation DetailViewController
- (void)dealloc{
    [_tableview release];
    [_message release];
    [_dataSource release];
    [super dealloc];
}

#pragma  mark - laze loading - 
- (UITableView *)tableview{
    if (!_tableview) {
        self.tableview = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)) style:UITableViewStyleGrouped] autorelease];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *naviTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(self.navigationItem.backBarButtonItem.width + 10, 0, 200, 44)];
    naviTitleLable.text = [NSString stringWithFormat:@"段子 %@",self.message.message_id];
    naviTitleLable.textAlignment = NSTextAlignmentCenter;
    naviTitleLable.font = [UIFont systemFontOfSize:15];
    self.navigationItem.titleView = naviTitleLable;



    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[CommentTableViewCell class] forCellReuseIdentifier:@"Comment"];
    NetWorkEngine *engine = [NetWorkEngine engineWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://m2.qiushibaike.com/article/%@/comments?count=50&page=1&AdID=1432516254311507B38D95",self.message.message_id]] parameters:nil delegate:self];
    [engine start];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentModel *comment = self.dataSource[indexPath.row];
    return comment.rowHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    TableViewCell *cell = [[TableViewCell alloc] initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, self.message.rowHeight)];
    [cell configureWithMessage:self.message];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.userButton.tag = 99;
    [cell.userButton addTarget:self action:@selector(handleUserButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell.contentView;

}






- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.message.rowHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Comment"];
    CommentModel *comment = self.dataSource[indexPath.row];
    [cell configCellWithModel:comment];
    cell.userButton.tag = indexPath.row + 100;
    [cell.userButton addTarget:self action:@selector(handleUserButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)networkDidFinishLoading:(NetWorkEngine *)engine withInfo:(id)info{
    NSDictionary *data = [NSJSONSerialization JSONObjectWithData:info options:NSJSONReadingMutableContainers error:nil];
    NSArray *items = data[@"items"];
    for (int i = 0; i < items.count; i++) {
        CommentModel *comment = [[[CommentModel alloc] initWithDictionary:items[i]] autorelease];
        [self.dataSource addObject:comment];
    }

    [self.tableview reloadData];
}

- (id)initWithContentView:(UIView *)contentView message:(MessageModel *)message{
    self = [super init];
    if (self) {
        self.contentView = contentView;
        self.message = message;
    }
    return self;
}

- (void)handleUserButtonAction:(UIButton *)sender{

    if (sender.tag == 99) {
        UserViewController *userVC = [[UserViewController alloc] initWithUser_id:self.message.user.uid];

        [self.navigationController pushViewController:userVC animated:YES];
        [userVC release];
        return;
    }

    UserViewController *userVC = [[UserViewController alloc] initWithUser_id:[(UserModel *)[self.dataSource[sender.tag - 100] user] uid]];

    [self.navigationController pushViewController:userVC animated:YES];
    [userVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
